package com.goddess.yeecolor.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.goddess.yeecolor.R;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.ToastUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 *修改密码
 */
public class ChagePassWordAcitivity extends BaseActivity implements View.OnClickListener{

    private String phone,number,password,againPassword;
    private EditText chagepassword_phone,chagepassword_number,chagepassword_password,chagepassword_againPassword;
    private TextView mTitle;
    private Toolbar mToobar;
    private Button chagepassword_save,chagepassword_send;
    private View mView;
    private TimeCount time;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        mView= LayoutInflater.from(this).inflate(R.layout.activity_chagepassword,null);
        setContentView(mView);
        initFindView();
        initOnClck();
    }
    @Override
    public void initOnClck() {
        chagepassword_save.setOnClickListener(this);
        chagepassword_send.setOnClickListener(this);
    }

    @Override
    public void initFindView() {
        time=new TimeCount(120*1000,1000);
        mToobar= (Toolbar) findViewById(R.id.title_toobar);
        mTitle= (TextView) findViewById(R.id.title_content);
        mTitle.setText("修改密码");
        mToobar.setNavigationIcon(R.mipmap.back3x);
        mToobar.setNavigationOnClickListener((view)->{
            setFiish(this);
        });
        chagepassword_save= (Button) findViewById(R.id.chagepassword_save);
        chagepassword_send= (Button) findViewById(R.id.chagepassword_send);
        chagepassword_phone= (EditText) findViewById(R.id.chagepassword_phone);
        chagepassword_number= (EditText) findViewById(R.id.chagepassword_number);
        chagepassword_password= (EditText) findViewById(R.id.chagepassword_password);
        chagepassword_againPassword= (EditText) findViewById(R.id.chagepassword_againPassword);
    }

    @Override
    public void initData() {

    }

    private Map<Object,Object>map;
    @Override
    public void onClick(View view) {
        int id=view.getId();
        switch (id){
           case  R.id.chagepassword_save://完成
               phone=chagepassword_phone.getText().toString().trim();
               number=chagepassword_number.getText().toString().trim();
               password=chagepassword_password.getText().toString().trim();
               againPassword=chagepassword_againPassword.getText().toString().trim();
               map=new HashMap<>();
               if (password.equals(againPassword)&&!againPassword.isEmpty()){
                   map.put("mobile",phone);
                   map.put("pwd",password);
                   map.put("code",number);
                   setRequest(MyUrl.url+MyUrl.chagePsd,map,handler,1);
               }
               else {
                   ToastUtils.toast("两次密码输入不一致");
               }

            break;
            case R.id.chagepassword_send://验证码

                phone=chagepassword_phone.getText().toString().trim();
                if(!phone.equals("")){
                    time.start();
                    map=new HashMap<>();
                    map.put("mobile",phone);
                    setRequest(MyUrl.url+MyUrl.chagePsd,map,handler,2);
                }else{
                    ToastUtils.toast("请输入密码");
                }

                break;
        }
    }

    private void setRequest(String url, Map<Object,Object>map,Handler handler,int tag){
        OkttpUtils.getIntence().OkttpPost(url,map,handler,tag);
    }
    private String jsonString;
    private int code;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                jsonString=msg.getData().getString("result");
                JSONObject object=new JSONObject(jsonString);

                String msgs=object.getString("msg");
                code = object.getInt("code");

                if (msg.what==1){
                    if (code==100){
                        ToastUtils.toast(msgs);
                        setFiish(ChagePassWordAcitivity.this);

                    }
                    else if (code==403){
                        ToastUtils.toast(msgs);
                    }
                }
                else if (msg.what==2){
                    if (code==100){
                        ToastUtils.toast(msgs);

                    }
                    else if (code==403){
                        ToastUtils.toast(msgs);
                    }
                }


            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    };
    /**
     * 设置时间
     */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);

        }
        @Override
        public void onTick(long l) {
            chagepassword_send.setClickable(false);
            chagepassword_send.setBackgroundResource(R.drawable.login_yanzm_button_select_back_false);
            chagepassword_send.setText("等待"+l/1000+"秒");
        }
        @Override
        public void onFinish() {
            chagepassword_send.setText("重新获取");
            chagepassword_send.setBackgroundResource(R.drawable.login_yanzm_button_back);
            chagepassword_send.setClickable(true);
        }
    }

}
